<template>
  <div class="homePage-container">
    <van-nav-bar fixed>
      <van-button
        class="search-btn"
        round
        slot="title"
        type="info"
        size="small"
        icon="search"
        to="/search"
      >
        搜索
      </van-button>
    </van-nav-bar>

    <!-- 频道列表 
       通过 animated 属性可以开启切换标签内容时的转场动画。
       通过 swipeable 属性可以开启滑动切换标签页。
     -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <!-- 文章列表 -->
        <article-list :channel="channel" />
      </van-tab>
      <!-- 频道列表编辑按钮 -->
      <div 
        class="more-btn"
        slot="nav-right"
        @click="isChannelEditShow = true">
        <van-icon name="wap-nav" />
      </div>
    </van-tabs>

    <!-- 频道列表编辑弹出层 -->
    <van-popup
      v-model="isChannelEditShow"
      closeable
      position="bottom"
      :style="{ height: '100%' }"
    >
    <channelEdit
     :my-channels="channels"
     :active="active"
     @update-active="onUpdateActive"/>
    </van-popup>
  </div>
</template>

<script>
import { getChannelsAPI } from "@/api";
import articleList from "./components/articleList.vue";
import channelEdit from "./components/channelEdit.vue";
export default {
  components: {
    articleList,
    channelEdit
  },
  data() {
    return {
      active: 0,
      channels: [], //频道列表
      isChannelEditShow:false //弹出层控制
    };
  },
  created() {
    this.loadChannels();
  },
  methods: {
    async loadChannels() {
      try {
        const { data } = await getChannelsAPI();
        this.channels = data.data.channels;
      } catch (err) {
        this.$toast("获取频道数据失败");
      }
    },
    onUpdateActive(index,isChannelEditShow = true){
      // 更新激活的频道项
      this.active = index

      // 关闭编辑频道弹层
      this.isChannelEditShow = false
    }
  },
};
</script>

<style socped lang="less">
.homePage-container {
  padding-bottom: 100px;
  padding-top: 174px;
  .search-btn {
    width: 450px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      display: flex;
      font-size: 53px;
    }
  }
.channel-tabs {
    .van-tabs__content{
      min-height: 79vh;

    }
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      z-index: 1;
      top: 92px;
      left: 0;
      right: 0;
    }
      /deep/ .van-tab {
      min-width: 200px;
      border-right: 1px solid #67a0c6;
     .van-tab__text{
       font-size: 30px;
      color: #333333;
     }
    }
    .van-tabs__line {
      width: 5%;
      background-color: rgb(117, 155, 231);
    }
    .more-btn {
      position: fixed;
      right: 0;
      display: flex;
      width: 66px;
      height: 82px;
      align-items: center;
      justify-content: center;
      background-color: #fff;
      opacity: 0.902;
      .van-icon{
        font-size: 42px;
      }
    }
  }
}
</style>